package flc.ast.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.TimeUtils;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.databinding.DialogBirthdayBinding;
import stark.common.basic.base.BaseSmartDialog;

public class BirthdayDialog extends BaseSmartDialog<DialogBirthdayBinding> implements View.OnClickListener {

    private List<String> listYear = new ArrayList<>();
    private List<String> listMonth = new ArrayList<>();
    private List<String> listDay = new ArrayList<>();

    public BirthdayDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_birthday;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.wheelView1.setDividerColor(Color.parseColor("#F0F0F0"));
        mDataBinding.wheelView1.setTextColorCenter(Color.parseColor("#2262D0"));
        mDataBinding.wheelView1.setTextColorOut(Color.parseColor("#CECECE"));
        mDataBinding.wheelView1.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView1.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView1.setCurrentItem(0);
        mDataBinding.wheelView1.setItemsVisibleCount(5);
        mDataBinding.wheelView1.setCyclic(false);
        mDataBinding.wheelView1.setTextSize(16);
        mDataBinding.wheelView2.setDividerColor(Color.parseColor("#F0F0F0"));
        mDataBinding.wheelView2.setTextColorCenter(Color.parseColor("#2262D0"));
        mDataBinding.wheelView2.setTextColorOut(Color.parseColor("#CECECE"));
        mDataBinding.wheelView2.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView2.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView2.setCurrentItem(0);
        mDataBinding.wheelView2.setItemsVisibleCount(5);
        mDataBinding.wheelView2.setCyclic(false);
        mDataBinding.wheelView2.setTextSize(16);
        mDataBinding.wheelView3.setDividerColor(Color.parseColor("#F0F0F0"));
        mDataBinding.wheelView3.setTextColorCenter(Color.parseColor("#2262D0"));
        mDataBinding.wheelView3.setTextColorOut(Color.parseColor("#CECECE"));
        mDataBinding.wheelView3.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView3.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView3.setCurrentItem(0);
        mDataBinding.wheelView3.setItemsVisibleCount(5);
        mDataBinding.wheelView3.setCyclic(false);
        mDataBinding.wheelView3.setTextSize(16);
        //设置滚轮数据
        listYear = new ArrayList<>();//年 1980-2040
        for (int i = 0; i < 61; i++) {
            listYear.add(String.valueOf(1980 + i) + "年");
        }
        listMonth = new ArrayList<>();//月 12
        for (int i = 0; i < 12; i++) {
            listMonth.add(String.valueOf(i + 1) + "月");
        }
        WheelAdapter adapter1 = new ArrayWheelAdapter(listYear);
        mDataBinding.wheelView1.setAdapter(adapter1);
        WheelAdapter adapter2 = new ArrayWheelAdapter(listMonth);
        mDataBinding.wheelView2.setAdapter(adapter2);
        setDayDate(31);//日 默认显示1月 共31天

        //监听年份滚动选择
        mDataBinding.wheelView1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (mDataBinding.wheelView2.getCurrentItem() == 1) {
                    if (TimeUtils.isLeapYear(listYear.get(mDataBinding.wheelView1.getCurrentItem()), new SimpleDateFormat("yyyy年"))) {
                        setDayDate(29);
                    } else {
                        setDayDate(28);
                    }
                }
            }
        });

        //监听月份滚动选择
        mDataBinding.wheelView2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                index = index + 1;
                if (index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 || index == 12) {
                    //大月31天
                    setDayDate(31);
                } else if (index == 2) {
                    //2月判断是闰(29)还是平(28)
                    if (TimeUtils.isLeapYear(listYear.get(mDataBinding.wheelView1.getCurrentItem()), new SimpleDateFormat("yyyy年"))) {
                        setDayDate(29);
                    } else {
                        setDayDate(28);
                    }
                } else {
                    //小月30天
                    setDayDate(30);
                }
            }
        });

        //滚动到对应日期
        String date = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"));
        String[] split = date.split("-");
        int year = Integer.parseInt(split[0]);//年
        int moon = Integer.parseInt(split[1]);//月
        int day = Integer.parseInt(split[2]);//日
        mDataBinding.wheelView1.setCurrentItem(year - 1980);
        mDataBinding.wheelView2.setCurrentItem(moon - 1);
        mDataBinding.wheelView3.setCurrentItem(day - 1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvCancel:
                dismiss();
                break;
            case R.id.tvConfirm:
                dismiss();
                if (listener != null) {
                    String str = listYear.get(mDataBinding.wheelView1.getCurrentItem()) + listMonth.get(mDataBinding.wheelView2.getCurrentItem()) + listDay.get(mDataBinding.wheelView3.getCurrentItem());
                    long time = TimeUtils.string2Millis(str, new SimpleDateFormat("yyyy年MM月dd日"));
                    listener.clickConfirm(TimeUtils.millis2String(time, new SimpleDateFormat("yyyy-MM-dd")));
                }
                break;
            default:
                break;
        }
    }


    //设置天
    private void setDayDate(int num) {
        listDay = new ArrayList<>();//日 默认显示1月31天
        for (int i = 0; i < num; i++) {
            listDay.add(String.valueOf(i + 1) + "日");
        }
        WheelAdapter adapter3 = new ArrayWheelAdapter(listDay);
        mDataBinding.wheelView3.setAdapter(adapter3);
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(String date);
    }
}
